package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
@Slf4j
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        log.debug("controller:{"+checkItem+"}");
        checkItemService.add(checkItem);
        log.debug("检查项添加成功");
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }


    @RequestMapping("/findByPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("controller: queryPageBean: ===>" + queryPageBean);
        PageResult pageResult = checkItemService.findByPage(queryPageBean);
        log.debug("检查项分页查询成功");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,pageResult);
    }

    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delById(Integer id){
        log.debug("controller: delById: id ===> " + id);
        checkItemService.delById(id);
        log.debug("删除检查项成功");
        return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id){
        log.debug("controller: findById: id ===> " + id);
        CheckItem checkItem = checkItemService.findById(id);
        log.debug("根据id查询检查项成功");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem){
        log.debug("controller: edit: checkItem ===> " + checkItem);
        checkItemService.edit(checkItem);
        log.debug("编辑检查项成功");
        return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        List<CheckItem> checkItemList = checkItemService.findAll();
        log.debug("controller: checkItemList: ===> "+checkItemList);
        log.debug("查询所有检查项成功");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }

}
